package gamegui;
import model.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import controller.PhysicsEngine;
import model.AIBar;
import model.Bar;
import model.Block;
import model.BreakableBlock;
import model.Map;
import model.Puck;
import model.SolidBlock;
import model.UserAccelerate;
import model.UserBar;
public class GamePlayPanel extends JPanel
{
	private final int WIDTH = 400, HEIGHT = 600;
	private final int DELAY = 50;
	private Timer timer;
	private Map map = new Map();
	private PhysicsEngine engine = new PhysicsEngine();
	private Dimension playPanelSize = new Dimension(500, 700);
	private Point playPanelLoc = new Point(103, 40);
	//-----------------------------------------------------------------
	// Sets up the panel, including the timer for the animation.
	//-----------------------------------------------------------------
	public GamePlayPanel()
	{
        setLocation(playPanelLoc);
        setSize(playPanelSize);
        setBackground(Color.red);
		//addKeyListener(new InputListener());
		timer = new Timer(DELAY, new TestListener());
		setFocusable(true);
		requestFocusInWindow();
		setPreferredSize (new Dimension(WIDTH, HEIGHT));
		setBackground (Color.WHITE);
		
		 Puck puck = new Puck();
         puck.setLocation(100, 100);
         System.out.println(puck.getX() + "  " + puck.getY());
         puck.setComponentX(20);
         puck.setComponentY(12);
         map.addObject(puck);
         
         Bar bar = new UserBar();
         bar.setLocation(170, 575);
         map.addObject(bar);
         
         bar = new AIBar();
         bar.setLocation(170, 0);
         map.addObject(bar);
                         
         Block block = new BreakableBlock(3);
         block.setLocation(50, 120);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(110, 145);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(170, 170);
         map.addObject(block);
         
         block = new SolidBlock();
         block.setLocation(230, 195);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(170, 220);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(110, 245);
         map.addObject(block);
         
         block = new SolidBlock();
         block.setLocation(50, 270);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(110, 295);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(170, 315);
         map.addObject(block);
         
         block = new BreakableBlock(3);
         block.setLocation(230, 340);
         map.addObject(block);
         
         Coin coin = new AIDisappear();
         coin.setLocation(120, 120);
         map.addObject(coin);
         
         coin = new AIShrink();
         coin.setLocation(230, 100);
         map.addObject(coin);
         
         coin = new UserAccelerate();
         coin.setLocation(20, 450);
         map.addObject(coin);
         
         coin = new ScoreIncrease();
         coin.setLocation(38, 150);
         map.addObject(coin);
         
         coin = new UserInvisibility();
         coin.setLocation(20, 300);
         map.addObject(coin);
         
         coin = new UserShrink();
         coin.setLocation(360, 300);
         map.addObject(coin);
         
         coin = new UserDecelerate();
         coin.setLocation(360, 150);
         map.addObject(coin);
                         
         coin = new ScoreDecrease();
         coin.setLocation(360, 100);
         map.addObject(coin);
                                                
                timer.start();

	}
	//-----------------------------------------------------------------
	// Draws the image in the current location.
	//-----------------------------------------------------------------
	public void paintComponent (Graphics page)
	{
		super.paintComponent (page);
		map.drawObjects(page);
	}
	
	private class TestListener implements ActionListener
	{
		public void actionPerformed (ActionEvent event)
		{
			for(int i = 0; i < 100; i++)
			{
				map.update(0);
				engine.detectAndHandleEdgeCollision(map);
				engine.detectAndHandleCollision(map);
			}
			repaint();
		}
	}
	
        public Map getMap(){
            return map;
        }
}